<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile, $size = NULL, $angle = NULL) {
  list($width, $height) = getimagesize($SourceFile);
  $image_p = imagecreatetruecolor($width, $height);
  $image = imagecreatefromjpeg($SourceFile);
  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
  //$black = imagecolorallocate($image_p, 0, 0, 0);
  $color = imagecolorallocatealpha($image_p, 255, 255, 255, 70);
  $font = drupal_get_path('module','win_observation') . '/fonts/calibrii.ttf';
  $font_size = 50;
  if ($size != NULL) {
    $font_size = $size;
  }
  $wm_angle = 0;
  if ($angle != NULL) {
    $wm_angle = $angle;
  }

  list($watermarkwidth, $watermarkheight) = getwatermarksize($font_size, $wm_angle, $font, $WaterMarkText);
  while ((($font_size - 10) > 20) && (($watermarkwidth > $width) || ($watermarkheight > $height))) {
    $font_size -= 10;
    list($watermarkwidth, $watermarkheight) = getwatermarksize($font_size, $angle, $font, $WaterMarkText);
  }
  $x = ($width / 2) - ($watermarkwidth / 2) + 15;
  $y = ($height / 2) + ($watermarkheight / 2);

  imagettftext($image_p, $font_size, $wm_angle, $x, $y, $color, $font, $WaterMarkText);
  if ($DestinationFile<>'') {
    imagejpeg ($image_p, $DestinationFile, 100);
  } else {
    header('Content-Type: image/jpeg');
    imagejpeg($image_p, null, 100);
  };
  imagedestroy($image);
  imagedestroy($image_p);
}

function getwatermarksize($font_size, $angle, $font, $WaterMarkText) {
  $width = 0;
  $height = 0;
  $watermarksize = ImageTTFBBox($font_size, $angle, $font, $WaterMarkText);
  /* $watermarksize =>
  Array
  (
      [0] => lower left X coordinate
      [1] => lower left Y coordinate
      [2] => lower right X coordinate
      [3] => lower right Y coordinate
      [4] => upper right X coordinate
      [5] => upper right Y coordinate
      [6] => upper left X coordinate
      [7] => upper left Y coordinate
  )
  */

  $watermarkbox = array();
  if ($watermarksize[0] < $watermarksize[6]) {
    $watermarkbox['xl'] = $watermarksize[0];
  } else {
    $watermarkbox['xl'] = $watermarksize[6];
  }
  if ($watermarksize[2] > $watermarksize[4]) {
    $watermarkbox['xr'] = $watermarksize[2];
  } else {
    $watermarkbox['xr'] = $watermarksize[4];
  }
  if ($watermarksize[5] < $watermarksize[7]) {
    $watermarkbox['yt'] = $watermarksize[5];
  } else {
    $watermarkbox['yt'] = $watermarksize[7];
  }
  if ($watermarksize[1] > $watermarksize[3]) {
    $watermarkbox['yb'] = $watermarksize[1];
  } else {
    $watermarkbox['yb'] = $watermarksize[3];
  }

  $width = $watermarkbox['xr'] - $watermarkbox['xl'];
  $height = $watermarkbox['yb'] - $watermarkbox['yt'];

  return array($width, $height);
  /*
  // for easy calculations, set the watermarkbox to xl & yt = 0
  $offset = $watermarkbox['xl'];
  $watermarkbox['xl'] -= $offset;
  $watermarkbox['xr'] -= $offset;
  $offset = $watermarkbox['yt'];
  $watermarkbox['yt'] -= $offset;
  $watermarkbox['yb'] -= $offset;
  */
}

/*
$SourceFile = 'image1.jpg';
$DestinationFile = 'image1-watermark.jpg';
$DestinationFile = '';
$WaterMarkText = 'Copyright Viraj Kanwade';
if (isset($_REQUEST['text']) && !empty($_REQUEST['text'])) {
  $WaterMarkText = utf8_encode($_REQUEST['text']);
}
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);
*/

function createImageThumbnail($filename, $tn_filename) {
  $thumbWidth = 100;

  // load image and get image size
  $img = imagecreatefromjpeg( $filename );
  $width = imagesx( $img );
  $height = imagesy( $img );

  // calculate thumbnail size
  $new_width = $thumbWidth;
  $new_height = floor( $height * ( $thumbWidth / $width ) );

  // create a new temporary image
  $tmp_img = imagecreatetruecolor( $new_width, $new_height );

  // copy and resize old image into new image
  imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

  // save thumbnail into a file
  imagejpeg( $tmp_img, $tn_filename );
}

function get_captcha_image($key = NULL)
{
  //Let's generate a totally random string using md5
  $md5_hash = md5(rand(0,999));
  //We don't need a 32 character long string so we trim it down to 5
  $security_code = substr($md5_hash, 15, 5);

  if ($key == NULL) {
    $key = "security_code";
  }
  
  //Set the session to store the security code
  $_SESSION[$key] = $security_code;

  //Set the image width and height
  $width = 120;
  $height = 70;

  //Create the image resource
  $image = ImageCreate($width, $height);

  //We are making three colors, white, black and gray
  $white = ImageColorAllocate($image, 255, 255, 255);
  $black = ImageColorAllocate($image, 0, 0, 0);
  $grey = ImageColorAllocate($image, 240, 240, 240);

  //Make the background black
  ImageFill($image, 0, 0, $black);

  /*
  $font = 'fonts/calibrii.ttf';
  $font_size = 50;
  $angle = 0;
  $x = 10;
  $y = 10;
  $color = imagecolorallocatealpha($image_p, 255, 255, 255, 70);
  imagettftext($image, $font_size, $angle, $x, $y, $color, $font, $security_code);
  */

  //Add randomly generated string in white to the image
  // $str = $security_code[0] . ' ' . $security_code[1] . ' ' . $security_code[2] . ' ' . $security_code[3] . ' ' . $security_code[4];
  // ImageString($image, 5, 0, 0, $str, $white);
  $d1 = 15;
  $delta = 8;
  $min = 2;
  $max = $d1;
  ImageString($image, rand(4, 5), rand($min, $max), rand(15, 40), $security_code[0], $white);
  $min = $max + $delta;
  $max = $min + $d1;
  ImageString($image, rand(4, 5), rand($min, $max), rand(15, 40), $security_code[1], $white);
  $min = $max + $delta;
  $max = $min + $d1;
  ImageString($image, rand(4, 5), rand($min, $max), rand(15, 40), $security_code[2], $white);
  $min = $max + $delta;
  $max = $min + $d1;
  ImageString($image, rand(4, 5), rand($min, $max), rand(15, 40), $security_code[3], $white);
  $min = $max + $delta;
  $max = $min + $d1;
  ImageString($image, rand(4, 5), rand($min, $max), rand(15, 40), $security_code[4], $white);

  //Throw in some lines to make it a little bit harder for any bots to break
  ImageRectangle($image,0,0,$width-1,$height-1,$grey);
  imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $grey);
  imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $grey);
  imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $grey);
  imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $grey);

  //Tell the browser what kind of file is come in
  header("Content-Type: image/jpeg");

  //Output the newly created image in jpeg format
  ImageJpeg($image);

  //Free up resources
  ImageDestroy($image);
}